import { forEach, isNil } from '../libs/utils';

class coreTable {
    constructor(){
        // this.dataset = [];
    }

    createDom(opts = {}){
        let tds = opts.tds;
        let cols = opts.cols;
        let rows = opts.rows;
        let tableLayout = opts.tableLayout || 'auto';
        
        let table = document.createElement('table');

        table.className = tableLayout === 'auto' ? 'alt-auto' : 'alt-fixed';

        let colgroup = this._createColGroup(cols, tableLayout);
        let theadTbody = this._createTheadTbody(rows, tds);

        table.appendChild(colgroup);
        table.appendChild(theadTbody);

        return table;
    }

    _createColGroup(cols, tableLayout){
        let colgroup = document.createElement('colgroup');
        if(tableLayout !== 'auto'){
            forEach(cols, function(colInfo){
                let col = document.createElement('col');
                let width = colInfo.size;
                if(!isNil(width)){
                    col.style.width = `${width}px`;
                }
                colgroup.appendChild(col);
            });
        }
        return colgroup;
    }

    _createTheadTbody(rows, dataset){
        let _this = this;
        let thead = document.createElement('thead');
        let tbody = document.createElement('tbody');

        forEach(rows, function(rowInfo, index){
            let tr = _this._createTrTh(rowInfo, dataset[index]);
            if(rowInfo.isHeader){
                thead.appendChild(tr);
            }else{
                tbody.appendChild(tr);
            }
        })
        let docFragment = document.createDocumentFragment();
        docFragment.appendChild(thead);
        docFragment.appendChild(tbody);
        return docFragment;
    }

    _createTrTh(rowInfo, rowData){
        let tr = document.createElement('tr');
        tr.className = getRowClass(rowInfo);
        tr.style = rowInfo.style || '';
        if(!isNil(rowInfo.size)){
            tr.style.height = `${rowInfo.size}px`;
        }
        tr.dataset.index = rowInfo.index;
        let childTag = rowInfo.isHeader ? 'th' : 'td';

        forEach(rowData, function(tdInfo){
            if(tdInfo.hidden) return;
            if(tdInfo._$el){
                tr.appendChild(tdInfo._$el);
                return;
            }
            me.createTd(tdInfo, childTag);
            tr.appendChild(tdInfo._$el);
        })

        rowInfo._$el = tr;
        return tr;
    }

    createTds(dataset, rows = []){
        let me = this;
        let tds = [];
        forEach(dataset, function(row, i){
            tds[i] = [];
            let rowInfo = rows[i];
            let childTag = rowInfo.isHeader ? 'th' : 'td';
            forEach(row, function(cell){
                me.createTd(cell, childTag);
                tds[i].push(cell);
            })
        })

        return tds;
    }

    createTd(cell, tag){
        if(cell._$el) return cell;
        let child = document.createElement(tag);
        child.dataset.row = cell.rowIndex;
        child.dataset.col = cell.colIndex;
        child.rowspan = cell.rowspan;
        child.colspan = cell.colspan;
        child.style = cell.style || '';
        child.className = getThTdClass(cell);
        child.innerText = cell.text;
        cell._$el = child;

        return cell;
    }
    
}
function getRowClass(rowInfo){
    let index = rowInfo.index;
    let defClass = index % 2 ? 'even' : 'odd';
    let customClass = rowInfo.classList ? rowInfo.classList.join(' ') : '';
    return defClass + ' ' + customClass;
}

function getThTdClass(info){
    let className = '';
    if(info.classList){
        className = info.classList.join(' ');
    }

    return className;
}

export default coreTable;